﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LibrarySync2.Controls
{
    public partial class ucFileSystemItemSelector : BaseUserControl
    {
        protected const string LibraryFileFilter = "POO Files|*.poo";
        protected const string BlacklistFileFilter = "SHT Files|*.sht";

        public enum FileItemSelectorType
        {
            File,
            Directory
        }

        public enum FileFilterType
        {
            Library,
            Blacklist
        }

        [Category("Other")]
        [Browsable(true)]
        [Description("Gets or Sets the prompt on the input.")]
        public string Prompt
        {
            get { return lblPrompt.Text; }
            set { lblPrompt.Text = value; }
        }

        [Category("Other")]
        [Browsable(true)]
        [Description("Gets or Sets the path selected.")]
        public string Path
        {
            get { return string.IsNullOrEmpty(txtInput.Text) ? string.Empty : txtInput.Text; }
            set { txtInput.Text = value; }
        }

        [Category("Other")]
        [Browsable(true)]
        [Description("Gets or Sets whether or not you can create a file.")]
        public bool AllowFileCreation
        {
            get { return !openFileDialog.CheckFileExists; }
            set { openFileDialog.CheckFileExists = !value; }
        }

        [Category("Other")]
        [Browsable(true)]
        [Description("Gets or Sets the type of selector.")]
        public FileItemSelectorType SelectorType { get; set; }

        [Category("Other")]
        [Browsable(true)]
        [Description("Gets or Sets the file type accepted.")]
        public FileFilterType FileType { get; set; }

        public ucFileSystemItemSelector()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            switch (SelectorType)
            {
                case FileItemSelectorType.Directory:
                    SelectDirectory();
                    break;
                case FileItemSelectorType.File:
                    SelectFile();
                    break;
                default:
                    throw new ApplicationException("Unhandled FileItemSelectorType: " + SelectorType.ToString());
            }
        }

        private void SelectDirectory()
        {
            folderBrowserDialog.SelectedPath = Path;
            var dialogResult = folderBrowserDialog.ShowDialog();
            if (dialogResult == DialogResult.OK)
                Path = folderBrowserDialog.SelectedPath;
        }

        private void SelectFile()
        {
            openFileDialog.FileName = Path;
            openFileDialog.Filter = GetFileFilter();
            openFileDialog.FilterIndex = 0;
            var dialogResult = openFileDialog.ShowDialog();
            if (dialogResult == DialogResult.OK)
                Path = openFileDialog.FileName;

        }

        private string GetFileFilter()
        {
            switch (FileType)
            {
                case FileFilterType.Blacklist :
                    return BlacklistFileFilter;
                case FileFilterType.Library:
                    return LibraryFileFilter;
                default:
                    throw new ApplicationException("Unhandled FileType: " + FileType.ToString());
            }
        }
    }
}
